package com.test;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.UUID;

/**
 * @author zhoajk
 */
public class ElasticViewer {

    public static void main(String[] args) {
        JSONArray jsonArray = listAllIndices("http://192.168.1.91:9200/_cat/indices");
        for (Object o : jsonArray) {
            System.out.println(((JSONObject) o).getStr("index"));
        }
    }

    public static JSONArray listAllIndices(String url) {
        if (!ckStr(url)) {
            return null;
        }
        String result = sendGet(url);
        if (StringUtils.isEmpty(result)) {
            return null;
        }
        String[] arr = result.split("\n");
        JSONArray jsonArray = new JSONArray();
        for (String s : arr) {
            String[] datas = s.split("\\s+");
            String index = datas[2];
            if (index.contains(".")) {
                continue;
            }
            JSONObject json = new JSONObject();
            json.put("health", datas[0]);
            json.put("status", datas[1]);
            json.put("index", index);
            json.put("pri", datas[3]);
            json.put("rep", datas[4]);
            json.put("docs.count", datas[5]);
            json.put("docs.deleted", datas[6]);
            json.put("score.size", datas[7]);
            json.put("pri.store.size", datas[8]);
            jsonArray.add(json);
        }
        return jsonArray;
    }

    private static String sendGet(String url) {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpGet get = new HttpGet(url);

        CloseableHttpResponse response;

        try {
            response = client.execute(get);
            HttpEntity entity = response.getEntity();
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == 200) {
                return EntityUtils.toString(entity);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 验证字符串是否符合规则
     *
     * @param url
     * @param reg
     * @return
     */
    public static boolean ckStr(String url, String reg) {
        if (StringUtils.isEmpty(url)) {
            return false;
        }
        if (StringUtils.isEmpty(reg)) {
            return true;
        }
        return url.matches(reg);
    }

    public static boolean ckStr(String url) {
        return ckStr(url, null);
    }

    /**
     * 获取随机id
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

}
